package com.seele.tigerwallet.ui.activtiy;

import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.api.APICallBack;
import com.seele.tigerwallet.api.APIException;
import com.seele.tigerwallet.api.APIListCallBack;
import com.seele.tigerwallet.api.APIModule;
import com.seele.tigerwallet.api.APIObjectCallBack;
import com.seele.tigerwallet.model.MessageModel;
import com.seele.tigerwallet.model.TsModel;
import com.seele.tigerwallet.model.TsRecord;
import com.seele.tigerwallet.model.UserInfo;
import com.seele.tigerwallet.ui.application.SeeleApplication;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.manager.DaoManager;
import com.seele.tigerwallet.ui.manager.GlideApp;
import com.seele.tigerwallet.ui.utils.SPUtils;

import org.w3c.dom.Text;

import java.util.List;

import retrofit2.Call;

/**
 * Description:交易记录界面
 * Data: 2018/6/9 17:31
 * author: 郑波
 */
public class RecordaActivity extends TitleBaseActivity {


    private MessageModel mMessageModel;
    private TsModel mTsModel;
    private ImageView mImageStatus;
    private TextView mRecordaStatus;// 交易状态
    private TextView mValue;         // 数量
    private TextView mFromAddress;  // 发款方
    private TextView mToAddress;    // 收款方
    private TextView mGas;           // 矿工费
    private TextView mContent;      // 备注
    private TextView mTsBlockNum;  // 区块号
    private TextView mTsNum;        // 交易号
    private TextView mTsTime;       // 交易时间
    private static String TAG = RecordaActivity.class.getSimpleName();

    @Override
    public int getContainerViewId() {
        return R.layout.activity_recorda;
    }

    @Override
    protected void initView() {
        setTitle(R.string.trade_record);
        Bundle bundle = getIntent().getExtras();
        mImageStatus = findViewById(R.id.iv_transfer_status);// 交易状态图标
        mRecordaStatus = findViewById(R.id.tv_transfer_state); // 交易状态
        mValue = findViewById(R.id.tv_token_value);         // 数量
        mFromAddress = findViewById(R.id.tv_from_address);  // 发款方
        mToAddress = findViewById(R.id.tv_to_address);    // 收款方
        mGas = findViewById(R.id.tv_gas_value);          // 矿工费
        mContent = findViewById(R.id.tv_content);    // 备注
        mTsBlockNum = findViewById(R.id.tv_transfer_block); // 区块号
        mTsNum = findViewById(R.id.tv_transfer_hash);     // 交易号
        mTsTime = findViewById(R.id.tv_transfer_time);        // 交易时间
        mTsModel = (TsModel)bundle.getSerializable(RecordaActivity.class.getSimpleName());
        TsRecord tsRecord = null;
        long tsid = 0;
        String tshash = "";
        if (mTsModel == null) { //从消息中心进入
            mMessageModel =  (MessageModel)bundle.getSerializable(MessageActivity.class.getSimpleName());
            tsRecord = queryTsRecord(mMessageModel.tshash);
            tsid = mMessageModel.tsid;
            tshash = mMessageModel.tshash;
        } else { //从转账记录进入
            tsRecord = queryTsRecord(mTsModel.tshash);
            tsid = mTsModel.tsid;
            tshash = mTsModel.tshash;
        }
        if (tsRecord != null) {
            setRecord(tsRecord);
            if (tsRecord.tsstatus == 3 || tsRecord.tsstatus == 5) {
                querySingleTsinfo(tsid, tshash);
            }
        } else {
            querySingleTsinfo(tsid, tshash);
        }

    }

    private void setImageView(ImageView view, int resId) {
        Drawable drawableLeft = getResources().getDrawable(resId);
        view.setImageDrawable(drawableLeft);
    }

    private void setRecord(TsRecord tsModel) {
        Log.i("psp", "setRecord");
        if (tsModel.tsstatus == 0) {
            mRecordaStatus.setText("转账失败");
            Drawable drawableLeft = getResources().getDrawable(R.drawable.failed);
            mImageStatus.setImageDrawable(drawableLeft);
        } else if (tsModel.tsstatus == 1) {
            mRecordaStatus.setText("转账成功");
            setImageView(mImageStatus, R.drawable.ic_successfulsvg);
        } else if (tsModel.tsstatus == 2) { //暂时不用
            //mRecordaStatus.setText("确认中");
            //Drawable drawableLeft = getResources().getDrawable(R.drawable.queued);
            //drawableLeft.setBounds(0, 0, 40,40);
            //mRecordaStatus.setCompoundDrawables(drawableLeft, null, null, null);
        }  else if (tsModel.tsstatus == 3) {
            mRecordaStatus.setText("等待打包");
            setImageView(mImageStatus, R.drawable.pendding);
        } else if (tsModel.tsstatus == 4) {
            mRecordaStatus.setText("打包失败");
            setImageView(mImageStatus, R.drawable.failed);
        } else if (tsModel.tsstatus == 5){
            mRecordaStatus.setText("确认中");
            setImageView(mImageStatus, R.drawable.confirming);
        }

        //Typeface font = Typeface.createFromAsset(SeeleApplication.getInstance().getAssets(), "fonts/CENTURY_GOTHIC_BOLD.TTF");
        //mValue.setTypeface(font);
        mValue.setText(tsModel.value + " " + tsModel.symbol);

        mFromAddress.setText(tsModel.tsfromaddr);
        //mFromAddress.setTypeface(font);
        mToAddress.setText(tsModel.tstoaddr);
       //mToAddress.setTypeface(font);
        mGas.setText(SPUtils.getGas(tsModel.minerfee )+ " ETH");
        //mGas.setTypeface(font);
        mContent.setText(tsModel.getNote());
        mTsBlockNum.setText(tsModel.tsblocknum + "");
        //mTsBlockNum.setTypeface(font);
        mTsNum.setText(tsModel.tshash);
        //mTsNum.setTypeface(font);
        mTsTime.setText(tsModel.tstime);
        //mTsTime.setTypeface(font);
    }

    /**
     * 查询单笔交易记录
     */
    private void querySingleTsinfo(long tsid, String tshash) {
        Log.i("psp", "querySingleTsinfo");
        showLoadingDialog();
        APIModule.getApi()
                .querySingleTsinfo(tsid,tshash)
                .enqueue(new APIObjectCallBack<TsRecord>() {
                    @Override
                    protected void onResponse(TsRecord response) {
                        Log.i(TAG, "getMsg onResponse");
                        setRecord(response);
                        dismissLoadingDialog();
                        DaoManager.saveOrUpdate(response);
                    }

                    @Override
                    protected void onResponseError(APIException e) {
                        Log.i(TAG, "getMsg onResponseError");
                        dismissLoadingDialog();
                    }

                    @Override
                    protected void onFailureError(Call<String> call, Throwable t) {
                        Log.i(TAG, "getMsg onFailureError");
                        dismissLoadingDialog();
                    }
                });

    }

    private TsRecord queryTsRecord(String tshash) {
        Log.i("psp", "queryTsRecord");
        List<TsRecord> tsRecord = null;
        try {
            tsRecord = DaoManager.getTsRecordDao().queryBuilder().where().eq("tshash", tshash).query();
        }catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
        if (tsRecord != null && tsRecord.size() == 1) {
            Log.i("psp", "queryTsRecord not null");
            return tsRecord.get(0);
        }
        Log.i("psp", "queryTsRecord null");
        return null;
    }


}
